package com.huluobo.a2111douyindemo.setting;

import androidx.lifecycle.MutableLiveData;

import com.huluobo.mvvm_base.mvvm.base.model.BaseRepo;
import com.huluobo.mvvm_base.mvvm.bean.SettingBeanResult;
import com.huluobo.mvvm_base.mvvm.bean.UserBeanResult;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MultipartBody;

/**
 * Created by LC on 2024/3/13.
 */
public class SettingRepo extends BaseRepo {
    public void updateAddress(String address, MutableLiveData<SettingBeanResult> success,
                              MutableLiveData<String> failed) {
        apiService.updateAddress(address)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<SettingBeanResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(SettingBeanResult settingBeanResult) {
                        success.setValue(settingBeanResult);
                    }

                    @Override
                    public void onError(Throwable e) {
                        failed.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void updateUserIcon(MultipartBody.Part part, MutableLiveData<SettingBeanResult> success,
                               MutableLiveData<String> failed) {
        apiService.updateIcon(part)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<SettingBeanResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(SettingBeanResult settingBeanResult) {
                        success.setValue(settingBeanResult);
                    }

                    @Override
                    public void onError(Throwable e) {
                        failed.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void updateNickname(String nickname, MutableLiveData<SettingBeanResult> success,
                               MutableLiveData<String> failed) {
        apiService.updateNickname(nickname)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<SettingBeanResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(SettingBeanResult settingBeanResult) {
                        success.setValue(settingBeanResult);
                    }

                    @Override
                    public void onError(Throwable e) {
                        failed.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void updateBrith(String brith, MutableLiveData<SettingBeanResult> success, MutableLiveData<String> failed) {
        apiService.updateBrith(brith)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<SettingBeanResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(SettingBeanResult settingBeanResult) {
                        success.setValue(settingBeanResult);
                    }

                    @Override
                    public void onError(Throwable e) {
                        failed.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void updateSex(String sex, MutableLiveData<SettingBeanResult> success, MutableLiveData<String> failed) {
        apiService.updateSex(sex)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<SettingBeanResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(SettingBeanResult settingBeanResult) {
                        success.setValue(settingBeanResult);
                    }

                    @Override
                    public void onError(Throwable e) {
                        failed.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void queryUserInfoByName(String username, MutableLiveData<UserBeanResult.UserBean> success,
                                    MutableLiveData<String> failed) {
        userDao.queryUserInfoByName(username)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<UserBeanResult.UserBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(UserBeanResult.UserBean userBean) {
                        success.setValue(userBean);
                    }

                    @Override
                    public void onError(Throwable e) {
                        failed.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
